﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using nRoute.Components;
using nRoute.Mapping;
using nRoute.ViewModels;
using SimpleShell.Views;

namespace SimpleShell.ViewModels
{
    [DefineNavigationContent("Content/PageB", typeof(PageBView))]
    [MapViewModel(typeof(PageBView))]
    public class PageBViewModel : NavigationViewModelBase
    {
        private bool _optionA;
        private bool _optionB;
        private bool _optionC;
        private string _message;
        private bool _isEnabled;

        public PageBViewModel()
        {
            // by default
            _optionA = true;
            SetupCommands();
        }

        public bool OptionA
        {
            get { return _optionA; }
            set
            {
                if (_optionA != value)
                {
                    _optionA = value;
                    base.NotifyPropertyChanged(() => OptionA);
                }
            }
        }

        public bool OptionB
        {
            get { return _optionB; }
            set
            {
                if (_optionB != value)
                {
                    _optionB = value;
                    base.NotifyPropertyChanged(() => OptionB);
                }
            }
        }

        public bool OptionC
        {
            get { return _optionC; }
            set
            {
                if (_optionC != value)
                {
                    _optionC = value;
                    base.NotifyPropertyChanged(() => OptionC);
                }
            }
        }

        public string Message
        {
            get { return _message; }
            set
            {
                if (_message != value)
                {
                    _message = value;
                    base.NotifyPropertyChanged(() => Message);
                }
            }
        }

        public bool IsEnabled
        {
            get { return _isEnabled; }
            set
            {
                if (_isEnabled != value)
                {
                    _isEnabled = value;
                    base.NotifyPropertyChanged(() => IsEnabled);
                }
            }
        }

        public ActionCommand UpdateMessageCommand { get; set; }

        protected override ParametersCollection OnSaveState()
        {
            var _state = new ParametersCollection();
            _state.Add("A", this.OptionA);
            _state.Add("B", this.OptionB);
            _state.Add("C", this.OptionC);
            _state.Add("M", this.Message);
            return _state;
        }

        protected override void OnRestoreState(ParametersCollection state)
        {
            if (state != null && state.Count > 0)
            {
                OptionA = state.GetValueOrDefault<bool>("A", true);
                OptionB = state.GetValueOrDefault<bool>("B");
                OptionC = state.GetValueOrDefault<bool>("C", true);
                Message = state.GetValueOrDefault<string>("M");
            }
        }

        private void SetupCommands()
        {
            UpdateMessageCommand = new ActionCommand(() =>
            {
                this.Message = "Message Updated at " + DateTime.Now.ToLocalTime();
            });
        }
    }
}
